﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UCL;

namespace UAS5
{
    /// <summary>
    /// 清除记录
    /// </summary>
    /// <remarks>2013-12-10 by sutroon</remarks>
    public partial class frmDeleteRecords : Form
    {
        AppDataHelper adh;
        public frmDeleteRecords()
        {
            InitializeComponent();
            adh = new AppDataHelper();
        }

        private void frmDeleteRecords_Load(object sender, EventArgs e)
        {
            this.cmsClickConfig();
            this.FormLoad();
        }

        private void FormLoad()
        {
            DataTable dtFiles = adh.ExecuteDataTable("select * from tquickdial");
            if (dtFiles.Rows.Count == 0) return;
            DataTable dtExists = dtFiles.CopySehcma();
            string path;
            foreach (DataRow r in dtFiles.Rows)
            {
                path = FileHelper.GetExistsPath(r["Paths"].ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                if (string.IsNullOrEmpty(path)) continue;
                if (path.Contains("360Cloud") || path.Contains("KuaipanCloud")) continue;
                dtExists.Rows.Add(r.ItemArray);
            }
            dgvList.DataSource = dtExists;
        }

        private void cmsClickConfig()
        {
            cmi打开目录ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi删除记录ToolStripMenuItem.Click += new EventHandler(cmi_Click);
            cmi刷新ToolStripMenuItem.Click += new EventHandler(cmi_Click);
        }

        private void cmi_Click(object sender, EventArgs e)
        {
            string path;
            switch (((ToolStripMenuItem)sender).Name)
            {
                case "cmi打开目录ToolStripMenuItem":
                    if (dgvList.CurrentCell == null) return;
                    path = FileHelper.GetExistsPath(dgvList.CurrentCell.OwningRow.Cells["Paths"].Value.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                    FileHelper.OpenFolder(path);
                    break;
                case "cmi删除记录ToolStripMenuItem":
                    if (dgvList.CurrentCell == null) return;
                    path = FileHelper.GetExistsPath(dgvList.CurrentCell.OwningRow.Cells["Paths"].Value.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries));
                    if (path.Length == 0)
                    {
                        adh.Delete("tquickdial", "ID=" + dgvList.CurrentCell.OwningRow.Cells["ID"].Value);
                        MessageBox.Show("已删除!");
                        this.FormLoad();
                    }
                    else
                    {
                        MessageBox.Show("目录有残留文件!");
                        FileHelper.OpenFolder(path);
                    }
                    break;
                case "cmi刷新ToolStripMenuItem":
                    this.FormLoad();
                    break;
            }
        }
    }
}
